package api

import (
	"fmt"
	"log"
	"mytest/initialize/database"
	"mytest/models"
	"mytest/models/request"
	"mytest/service/system"
	"net/http"

	"github.com/gin-gonic/gin"
)

//必填 "路由path, 父菜单ID, 路由name, 对应前端文件路径, 排序标记"
func AddBaseMenu(c *gin.Context) {
	var form models.SysBaseMenu
	if e := c.Bind(&form); e != nil {
		c.String(http.StatusBadRequest, "bad request: %v", e.Error())
		return
	}
	if form.Path == "" || form.Name == "" {
		c.String(http.StatusOK, "传入参数path 或者 name ")
		return
	}
	log.Println("create_menu_form", form)
	db := database.GetDB()
	e := system.AddBaseMenu(db, form)
	if e != nil {
		c.JSON(http.StatusOK, UniversalResp{
			Code: 1,
			Msg:  fmt.Sprintf("菜单插入失败: %v", e.Error()),
		})

		return
	}

	c.JSON(http.StatusOK, UniversalResp{
		Data: form,
	})
}

//更新基础菜单,更新基础字段，如果更新新的外键，先删除外键，然后外键自增,
func UpdateBaseMenu(c *gin.Context) {
	var form models.SysBaseMenu
	if e := c.Bind(&form); e != nil {
		c.String(http.StatusBadRequest, "bad request: %v", e.Error())
		return
	}
	if form.ID == 0 {
		c.String(http.StatusOK, "传入参数id")
		return
	}
	log.Println("create_menu_form", form)
	db := database.GetDB()
	err, res := system.UpdateMenu(db, form)
	if err != nil {
		c.JSON(http.StatusOK, UniversalResp{
			Code: 1,
			Msg:  fmt.Sprintf("菜单插入失败: %v", err.Error()),
		})

		return
	}

	c.JSON(http.StatusOK, UniversalResp{
		Data: res,
	})
}

//删除基础菜单
func DeleteBaseMenu(c *gin.Context) {
	var form request.GetById
	if e := c.Bind(&form); e != nil {
		c.String(http.StatusBadRequest, "bad request: %v", e.Error())

		return
	}
	if form.Id == 0 {
		c.String(http.StatusOK, "输入菜单id")
		return
	}
	db := database.GetDB()
	e := system.DeleteBaseMenu(db, form.Id)
	if e != nil {
		c.JSON(http.StatusOK, UniversalResp{
			Code: 1,
			Msg:  fmt.Sprintf("删除菜单失败: %v", e.Error()),
		})
		return
	}

	c.JSON(http.StatusOK, UniversalResp{
		Msg: "success",
	})
}

//查看一个菜单
func GetBaseMenu(c *gin.Context) {
	var form request.GetById
	if e := c.Bind(&form); e != nil {
		c.String(http.StatusBadRequest, "bad request: %v", e.Error())

		return
	}
	if form.Id == 0 {
		c.String(http.StatusOK, "输入菜单id")
		return
	}
	log.Println(form)
	db := database.GetDB()
	e, res := system.GetBaseMenu(db, form.Id)
	if e != nil {
		c.JSON(http.StatusOK, UniversalResp{
			Code: 1,
			Msg:  fmt.Sprintf("get菜单失败: %v", e.Error()),
		})
		return
	}

	c.JSON(http.StatusOK, UniversalResp{
		Data: res,
	})
}

// 得到所有的 菜单,树形结构
func GetAllBaseMenu(c *gin.Context) {
	//log.Println("all_menu_form", form)

	db := database.GetDB()
	e, menus := system.GetAllMenus(db)
	if e != nil {
		c.JSON(http.StatusOK, UniversalResp{
			Code: 1,
			Msg:  fmt.Sprintf("菜单获取失败: %v", e.Error()),
		})
		return
	}

	c.JSON(http.StatusOK, UniversalResp{
		Data: menus,
	})
}

// 菜单列表，平级结构

func GetMenuList(c *gin.Context) {
	var req request.MenuList
	if e := c.Bind(&req); e != nil {
		c.String(http.StatusBadRequest, "bad request: %v", e.Error())

		return
	}
	db := database.GetDB()
	e, menus, total := system.GetMenuList(db, req)
	if e != nil {
		c.JSON(http.StatusOK, UniversalResp{
			Code: 1,
			Msg:  fmt.Sprintf("菜单获取失败: %v", e.Error()),
		})
		return
	}

	c.JSON(http.StatusOK, UniversalResp{
		Data: gin.H{
			"total": total,
			"data":  menus,
		},
	})
}

// 设置角色的 菜单
func SetAuthMenu(c *gin.Context) {
	var form request.ReqSetAuthMenu
	if e := c.Bind(&form); e != nil {
		c.String(http.StatusBadRequest, "bad request: %v", e.Error())

		return
	}
	if form.Auth == "" {
		c.String(http.StatusOK, "输入角色id")
		return
	}
	db := database.GetDB()
	e := system.SetAuthorityMenus(db, form.Auth, form.Menu)
	if e != nil {
		c.JSON(http.StatusOK, UniversalResp{
			Code: 1,
			Msg:  fmt.Sprintf("角色 设置菜单失败: %v", e.Error()),
		})

		return
	}

	c.JSON(http.StatusOK, UniversalResp{
		Msg: "success",
	})
}

//角色拥有的树形菜单
func GetMenusTreeForUser(c *gin.Context) {
	var form request.ReqSetAuthMenu
	if e := c.Bind(&form); e != nil {
		c.String(http.StatusBadRequest, "bad request: %v", e.Error())

		return
	}
	if form.Auth == "" {
		c.String(http.StatusOK, "输入角色id")
		return
	}

	db := database.GetDB()
	e, menus := system.GetMenusTreeForUser(db, form.Auth)
	if e != nil {
		c.JSON(http.StatusOK, UniversalResp{
			Code: 1,
			Msg:  fmt.Sprintf("获取角色的菜单失败: %v", e.Error()),
		})

		return
	}
	c.JSON(http.StatusOK, UniversalResp{
		Data: menus,
	})
}

//角色的拥有的列表结构菜单
func GetMenuAuthority(c *gin.Context) {
	var form request.ReqSetAuthMenu
	if e := c.Bind(&form); e != nil {
		c.String(http.StatusBadRequest, "bad request: %v", e.Error())

		return
	}
	if form.Auth == "" {
		c.String(http.StatusOK, "输入角色id")
		return
	}

	db := database.GetDB()
	e, menus := system.GetMenuAuthority(db, form.Auth)
	if e != nil {
		c.JSON(http.StatusOK, UniversalResp{
			Code: 1,
			Msg:  fmt.Sprintf("角色 设置菜单失败: %v", e.Error()),
		})

		return
	}

	c.JSON(http.StatusOK, UniversalResp{
		Data: menus,
	})
}
